from flask import Flask
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from geoalchemy2 import Geometry
from sqlalchemy import Column, Integer, TIMESTAMP

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://postgres:postgresql@localhost:5432/crowdsourcingDB"
db = SQLAlchemy(app)
migrate = Migrate(app, db)


class TasksModel(db.Model):
    __tablename__ = 'task'
    id = Column(Integer, primary_key=True)
    the_geom = Column(Geometry(geometry_type='POINT', management=True))
    type = Column(Integer)
    time = Column(TIMESTAMP)


@app.route('/tasks', methods=['GET'])
def handle_tasks():
    tasks = TasksModel.query.all()
    results = [
        {
            "id": task.id,
            # "the_geom": task.the_geom,
            "type": task.type,
            "time": task.time
        } for task in tasks]

    return {"count": len(results), "tasks": results}
